#include"maze.h"


MazeData * readMazeFromFile(const char * filename){

    FILE * fptr = fopen(filename,"r");
    if(NULL == fptr){

        printf("open file %s failed.\n",filename);
        return NULL;
    }
    // -这里分配内存之后要做判断
    MazeData * data = (MazeData*)malloc(sizeof(MazeData));
    char charactor;
    int row = 0;
    int col = 0;
    do{
        charactor = fgetc(fptr);
        switch(charactor){
            case 'E':
                data->endRow = row;
                data->endCol = col;
            case 'S':
                data->startRow = row;
                data->startCol = col;
        }
        if(' ' == charactor || 'E' == charactor || 'S' == charactor || '*' == charactor){

            col++;
        }
        // 最后一行
        if('\n' == charactor || feof(fptr)){
            row++;
            data->row = row;
            data->col = col;
            col = 0;
        }
    }while(!feof(fptr));
    mazeMap(fptr,data);
    // printf("row = %d, col = %d.\n",data->row,data->col);
    printf("start row = %d,start col = %d.\n",data->startRow,data->startCol);
    // printf("end row = %d,end col = %d.\n",data->endRow,data->endCol);
    return data;
}

void mazeMap(FILE * fptr,MazeData * data){

    // -跳转到文件的开头
    fseek(fptr,0,SEEK_SET);
    data->cell =(char**)malloc(sizeof(char*) * data->row);
    for(int row = 0;row < data->row;row++){

        data->cell[row] = (char*)malloc(sizeof(char)*data->col);
        for(int col = 0;col < data->col;col++){

            data->cell[row][col] = fgetc(fptr);
        }
        fgetc(fptr);    // -read '\n'
    }
    printMaze(data);
}
// -打印迷宫
void printMaze(MazeData * data){

    for(int row = 0;row < data->row;row++){

        for(int col = 0;col < data->col;col++){

            printf("%c",data->cell[row][col]);
        }
        printf("\n");
    }
}

// -利用递归进行回溯 参数分别为迷宫结构体，当前的行数，当前的列数 以及上次移动的方向 mode表示当前的状态 是请进 还是 回退
void getOut(MazeData * data,int row,int col,int direction,int * mode){

    // -终点判断
    if(row == data->endRow && col == data->endCol){

        printf("found end point.\n");
        (*mode) = NONE;
        return;
    }
    // -向东 direction!=WEST 表示上次的步骤不是相对的，避免在东西向 以及 南北向来回重复
    if(judyDirection(data,row,col,EAST) && direction!=WEST && NONE!=(*mode)){

        // -表示要东进
        (*mode) = FORWARD;
        printf("move(%d,%d).\n",row,col+1);
        getOut(data,row,col+1,EAST,mode);
        if(BACKWARD == (*mode)){

            printf("back to move(%d,%d).\n",row,col);
        }
    }
    if(judyDirection(data,row,col,SOUTH) && direction!=NORTH && NONE!=(*mode)){
        // -表示要南进
        (*mode) = FORWARD;
        printf("move(%d,%d).\n",row+1,col);
        getOut(data,row+1,col,SOUTH,mode);
        if(BACKWARD == (*mode)){

            printf("back to move(%d,%d).\n",row,col);
        }
    }
    if(judyDirection(data,row,col,WEST) && direction!=EAST && NONE!=(*mode)){
        // -表示要西进
        (*mode) = FORWARD;
        printf("move(%d,%d).\n",row,col-1);
        getOut(data,row,col-1,WEST,mode);
        if(BACKWARD == (*mode)){

            printf("back to move(%d,%d).\n",row,col);
        }
    }
    if(judyDirection(data,row,col,NORTH) && direction!=SOUTH && NONE!=(*mode)){
        // -表示要北进
        (*mode) = FORWARD;
        printf("move(%d,%d).\n",row-1,col);
        getOut(data,row-1,col,NORTH,mode);
        if(BACKWARD == (*mode)){

            printf("back to move(%d,%d).\n",row,col);
        }
    }
    if(NONE!=(*mode)){

        (*mode) = BACKWARD;
    }
}

int judyDirection(MazeData * data,int row,int col,int direction){

    switch(direction){

        case EAST:
            col++;
            break;
        case SOUTH:
            row++;
            break;
        case WEST:
            col--;
            break;
        case NORTH:
            row--;
            break;
    }
    if(row >= data->row || col >= data->col){

        return 0;
    }
    // -通路和终点
    if(' ' == data->cell[row][col] || 'E' == data->cell[row][col]){

        return 1;
    }
    // -死路
    return 0;
}